<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\Gift;
use app\home\model\Giftrecord;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Giftsrecord extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->param();
        $data['listrow'] = 30;
        $gift = new Giftrecord();
        $res = $gift->lists($data);
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(0,'无数据');
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
        
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 查看我收到的礼物

     */
    public  function getUserGiftlist(Request $request)
    {
        $data = $request->param();
        $validate = validate('Giftrecord');
        $res = $validate->scene('usergiftlist')->check($data);
        if (!$res){
            return apistatus(0,$validate->getError());
        }
        isset($data['listrow']) ? $data['listrow'] : $data['listrow'] = 12;
        $where['id'] = $data['id'];
        $rec = new Giftrecord();
        $list = $rec->alias('record')
            ->join('home_gifts F','record.giftid = F.id ')
            ->join('home_users U','record.uid = U.id')
            ->join('home_users G','record.givenid = G.id')
            ->field('record.id,record.uid,U.nickname uname,record.givenid,G.nickname gname,record.giftid,F.giftname,record.create_time')
            ->where('givenid',$where['id'])
            ->order('create_time desc')
            ->paginate($data['listrow']);
        if (!empty($list)){
            return apistatus(1,'列表获取成功',$list);
        }else{
            return apistatus(0,'无赠送记录');
        }
    }


    /**
     * 查看我收到的礼物

     */
    public  function getgiveGiftlist(Request $request)
    {
        $data = $request->param();
        $validate = validate('Giftrecord');
        $res = $validate->scene('usergiftlist')->check($data);
        if (!$res){
            return apistatus(0,$validate->getError());
        }
        isset($data['listrow']) ? $data['listrow'] : $data['listrow'] = 12;
        $where['id'] = $data['id'];
        $rec = new Giftrecord();
        $list = $rec->alias('record')
            ->join('home_gifts F','record.giftid = F.id ')
            ->join('home_users U','record.uid = U.id')
            ->join('home_users G','record.givenid = G.id')
            ->field('record.id,record.uid,U.nickname uname,record.givenid,G.nickname gname,record.giftid,F.giftname,record.create_time')
            ->where('uid',$where['id'])
            ->order('create_time desc')
            ->paginate($data['listrow']);
        if (!empty($list)){
            return apistatus(1,'列表获取成功',$list);
        }else{
            return apistatus(0,'无赠送记录');
        }
    }



}
